package com.ddwl.gate.mq.receive;

import com.alibaba.fastjson.JSONObject;
import com.ddwl.common.config.DingTalkNotify;
import com.ddwl.common.constant.RabbitMqConstants;
import com.ddwl.schema.vo.funds.OrderPaymentVo;
import com.ddwl.schema.vo.funds.OrderRefundsVo;
import com.ddwl.gate.service.MobileRechargeRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author: hemiaojun
 * @Date: 2020/9/26
 * @Description:
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class PayCallBackReceive {

    private final MobileRechargeRecordService mobileRechargeRecordService;
    private final DingTalkNotify dingTalkNotify;

    @RabbitListener(bindings = {@QueueBinding(value = @Queue(value = RabbitMqConstants.MOBILE_RECHARGE_NOTIFY_QUEUE, durable = "true"),
            exchange = @Exchange(value = RabbitMqConstants.MOBILE_RECHARGE_NOTIFY_EXCHANGE), key = RabbitMqConstants.MOBILE_RECHARGE_NOTIFY_QUEUE)})
    public void orderPayCallBack(String orderPayVo) {
        log.info("下单回调 OrderPayVo: {}", orderPayVo);
        try {
            mobileRechargeRecordService.orderPayCallBack(JSONObject.parseObject(orderPayVo, OrderPaymentVo.class));
        } catch (Exception e) {
            log.error("PayCallBackReceive.orderPayCallBack error", e);
            dingTalkNotify.exceptionNotify(e);
        }
    }

    @RabbitListener(bindings = {@QueueBinding(value = @Queue(value = RabbitMqConstants.MOBILE_RECHARGE_REFUND_NOTIFY_QUEUE, durable = "true"),
            exchange = @Exchange(value = RabbitMqConstants.MOBILE_RECHARGE_REFUND_NOTIFY_EXCHANGE), key = RabbitMqConstants.MOBILE_RECHARGE_REFUND_NOTIFY_QUEUE)})
    public void orderRefundCallBack(String vo) {
        log.info("退款回调 OrderRefundsVo: {}", vo);
        try {
            mobileRechargeRecordService.orderRefundCallBack(JSONObject.parseObject(vo, OrderRefundsVo.class));
        } catch (Exception e) {
            log.error("PayCallBackReceive.orderRefundCallBack error", e);
            dingTalkNotify.exceptionNotify(e);
        }
    }


    @RabbitListener(bindings = {@QueueBinding(value = @Queue(value = RabbitMqConstants.EXCEPTION_NOTIFY_QUEUE, durable = "true"),
            exchange = @Exchange(value = RabbitMqConstants.EXCEPTION_EXCHANGE), key = RabbitMqConstants.EXCEPTION_NOTIFY_QUEUE)})
    public void exceptionNotifyQueue(String msg) {
        log.info("错误日志通知队列 msg: {}", msg);
        try {
            JSONObject jsonObject = JSONObject.parseObject(msg);
            dingTalkNotify.exceptionNotify(jsonObject.getString("appName"),
                    jsonObject.getString("stack"),
                    jsonObject.getString("traceId"),
                    jsonObject.getString("profileActive"));
        } catch (Exception e) {
            log.error("错误日志通知消息消费失败 error", e);
            dingTalkNotify.exceptionNotify(e);
        }
    }
}



